package com.cdu.controller;

import com.cdu.commons.MallConstants;
import com.cdu.commons.R;
import com.cdu.commons.ServiceException;
import com.cdu.pojo.dto.OrderCreateDTO;
import com.cdu.pojo.entity.User;
import com.cdu.pojo.vo.OrderVO;
import com.cdu.pojo.vo.PageVO;
import com.cdu.service.OrderService;
import com.cdu.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ProjectName: cdu-app
 * @Titile: OrderController
 * @Author: Administrator
 * @Description: 订单控制类
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping
    public R<Void> createOrder(@RequestBody OrderCreateDTO orderCreateDTO, HttpServletRequest request) {
        String token = request.getHeader(MallConstants.TOKEN_SIGN);
        User user = null;
        if (StringUtils.hasLength(token)) {
            user = JwtUtils.parseToken(token);
        }
        if (user == null) {
            return R.ok("用户未登录"); // 保持一致性，也返回 ok
        }
        try {
            orderService.createOrder(orderCreateDTO, user);
            return R.ok("创建订单成功");
        } catch (ServiceException e) {
            return R.fail(e);
        }
    }

    /**
     * 根据ID获取订单详情
     */
    @GetMapping("/{Id}/get")
    public R<OrderVO> getOrderById(@PathVariable Integer Id) {
        try {
            OrderVO orderVO = orderService.getOrderById(Id);
            return R.ok("获取订单详情成功", orderVO);
        } catch (ServiceException e) {
            return R.fail(e);
        }
    }


    /**
     * 更新订单状态
     */
    @PostMapping("/{orderId}/status")
    public R<Void> updateOrderStatus(@PathVariable Integer orderId, @RequestParam Integer status, HttpServletRequest request) {
        String token = request.getHeader(MallConstants.TOKEN_SIGN);
        User user = null;
        if (StringUtils.hasLength(token)) {
            user = JwtUtils.parseToken(token);
        }
        if (user == null) {
            return R.ok("用户未登录");
        }
        try {
            orderService.updateOrderStatus(orderId, status, user);
            return R.ok("更新订单状态成功");
        } catch (ServiceException e) {
            return R.fail(e);
        }
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{orderId}/del")
    public R<Void> deleteOrder(@PathVariable Integer orderId, HttpServletRequest request) {
        String token = request.getHeader(MallConstants.TOKEN_SIGN);
        User user = null;
        if (StringUtils.hasLength(token)) {
            user = JwtUtils.parseToken(token);
        }
        if (user == null) {
            return R.ok("用户未登录");
        }
        try {
            orderService.deleteOrder(orderId, user);
            return R.ok("删除订单成功");
        } catch (ServiceException e) {
            return R.fail(e);
        }
    }



}